home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 43.zip / Sources C- WorkDisk V.adf / ex / region.c < prev    next >
C/C++ Source or Header  |  1987-02-15  |  4KB  |  190 lines

  1. /* region.c */
  2.  
  3. /* SIMPLE REGIONS EXAMPLE.... DRAW BEHIND A FENCE */
  4. /* Certain layers.library routines are used herein that aren't available
  5.  * until Amiga C compiler version 1.1 and beyond.  */
  6.  
  7. /* Author:  Rob Peck, 12/1/85
  8.  *
  9.  * This code may be freely utilized to create programs for the Amiga. 
  10.  */
  11.  
  12.  
  13. #include <exec/types.h>
  14. #include <graphics/gfx.h>
  15. #include <hardware/dmabits.h>
  16. #include <hardware/custom.h>
  17. #include <graphics/gfxmacros.h>
  18. #include <graphics/regions.h>
  19. #include <graphics/clip.h>
  20. #include <graphics/text.h>
  21. #include <hardware/blit.h>
  22. #include <graphics/gfxbase.h>
  23. #include <graphics/copper.h>
  24. #include <graphics/gels.h>
  25. #include <graphics/rastport.h>
  26. #include <graphics/view.h>
  27. #include <exec/exec.h>
  28. #include <graphics/layers.h>
  29.  
  30. #define FLAGS LAYERSIMPLE
  31. extern struct Layer *CreateUpfrontLayer();
  32.  
  33. struct GfxBase *GfxBase;
  34.  
  35. long LayersBase;
  36.  
  37. #define DEPTH 2  
  38. #define WIDTH 320 
  39. #define HEIGHT 200 
  40. #define NOT_ENOUGH_MEMORY -1000
  41. #define FOREVER for(;;) 
  42.  
  43. struct View *oldview;
  44. struct View v;
  45. struct ViewPort vp;
  46. struct ColorMap *cm;
  47. struct RasInfo ri;
  48. struct BitMap b;
  49. struct RastPort *rp;      /* one rastport for one layer */
  50.  
  51. short i,j,k,n;
  52. struct ColorMap *GetColorMap();
  53.  
  54. USHORT colortable[] = { 0x000, 0xf00, 0x0f0, 0x00f };
  55.          /* black, red, green, blue */
  56. UBYTE *displaymem;
  57. UWORD *colorpalette;
  58.  
  59. struct Layer_Info *li;
  60. struct Layer *layer;      /* one layer pointer */
  61.  
  62. extern struct Region *NewRegion();
  63. struct Region *rgn;      /* one region pointer */
  64. struct Rectangle rect[14];   /* some rectangle structures */
  65. struct Region *oldDamageList;
  66.  
  67. extern struct Layer_Info *NewLayerInfo();
  68.  
  69. main()
  70. {
  71.    SHORT x,y;
  72.  
  73.    GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0);
  74.    if (GfxBase == NULL) exit(1);
  75.    LayersBase = OpenLibrary("layers.library",0); 
  76.    if(LayersBase == NULL) exit(2);
  77.  
  78.  
  79.    oldview = GfxBase->ActiView;
  80.  
  81.    li = NewLayerInfo();    /* v1.1 compiler only */
  82.    InitView(&v);
  83.    v.ViewPort = &vp;
  84.    InitVPort(&vp);
  85.    vp.DWidth = WIDTH;
  86.    vp.DHeight = HEIGHT;
  87.    vp.RasInfo = &ri;
  88.    InitBitMap(&b,DEPTH,WIDTH,HEIGHT);
  89.    ri.BitMap = &b;
  90.    ri.RxOffset = 0;   
  91.    ri.RyOffset = 0;
  92.    ri.Next = NULL;
  93.    cm = GetColorMap(4);   
  94.    colorpalette = (UWORD *)cm->ColorTable;
  95.    for(i=0; i<4; i++)
  96.       *colorpalette++ = colortable[i];
  97.    vp.ColorMap = cm;   
  98.    for(i=0; i<DEPTH; i++)
  99.    {
  100.            b.Planes[i] = (PLANEPTR)AllocRaster(WIDTH,HEIGHT);
  101.            if(b.Planes[i] == NULL) exit(NOT_ENOUGH_MEMORY);
  102.    }
  103.  
  104.    MakeVPort( &v, &vp );   
  105.    MrgCop( &v );      
  106.    for(i=0; i<2; i++)
  107.       {
  108.       displaymem = (UBYTE *)b.Planes[i];
  109.       for(j=0; j<RASSIZE(WIDTH,HEIGHT); j++)
  110.          *displaymem++ = 0;   
  111.       /* zeros to all bytes of the display area */               }
  112.  
  113.    LoadView(&v);
  114.    layer = CreateUpfrontLayer(li,&b,0,0,200,140,FLAGS,NULL);
  115.    if(layer==NULL) exit(3);
  116.  
  117.    rp = layer->rp;
  118.  
  119.    SetAPen(rp,3);
  120.    RectFill(rp,0,0,199,139);   /* show the layer itself */
  121.  
  122.    j=10;         /* initialize the rectangles */ 
  123.    for(i=0; i<10; i++)
  124.       {
  125.       rect[i].MinX = j;
  126.       rect[i].MaxX = j + 8;
  127.       rect[i].MinY = 20;
  128.       rect[i].MaxY = 120;
  129.       j += 16;
  130.       }   
  131.       
  132.    rgn = NewRegion();   /* get a new region to use */
  133.    if(rgn == NULL) exit(4);
  134.  
  135.    for(i=0; i<14; i++)
  136.       OrRectRegion(rgn,&rect[i]);
  137.  
  138.    oldDamageList = layer->DamageList;
  139.    layer->DamageList = rgn;
  140.  
  141.    BeginUpdate(layer);
  142.  
  143.    /* here insert the drawing routines to draw something
  144.          * behind the slats.
  145.          */
  146.    x = 4;  y = 10;
  147.    SetAPen(rp,0);
  148.    SetDrMd(rp,JAM1);
  149.    RectFill(rp,0,0,199,139);
  150.    SetAPen(rp,1);
  151.    SetBPen(rp,0);
  152.    SetDrMd(rp,JAM2);
  153.    for(i=0; i<14; i++)
  154.    {
  155.       Move(rp, x, y);
  156.       Text(rp,"Behind A Fence",14);
  157.       x += 4;  y += 9;
  158.    }
  159.    EndUpdate(layer);
  160.    layer->DamageList = oldDamageList;
  161.    DisposeRegion(rgn);
  162.  
  163.    Delay(300);
  164.  
  165.    DeleteLayer(li, layer);
  166.    DisposeLayerInfo(li);
  167.  
  168.    LoadView(oldview);
  169.  
  170.    FreeMemory();   
  171.    CloseLibrary(GfxBase);
  172.  
  173. }    /* end of main() */
  174.  
  175.  
  176. FreeMemory()
  177. {            /* return user and system-allocated memory to sys manager */
  178.  
  179.      for(i=0; i<DEPTH; i++)         /* free the drawing area */
  180.            FreeRaster(b.Planes[i],WIDTH,HEIGHT);
  181.    FreeColorMap(cm);         /* free the color map */
  182.       /* free dynamically created structures */
  183.    FreeVPortCopLists(&vp);         
  184.    FreeCprList(v.LOFCprList);
  185.    return(0);
  186. }
  187.  
  188.  
  189.  
  190.